package org.tacademy.mytagwriter;


import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.common.base.Charsets;
import com.google.common.primitives.Bytes;

public class MyNFCTagWriterActivity extends Activity {

	EditText messageInput;
	TextView messageOutput;
	TextView tagOutput;
	
	RadioGroup rgroup01;
	RadioButton rbutton01;	//text type
	RadioButton rbutton02;	//uri type
	
	public static final int TYPE_TEXT = 1;
	public static final int TYPE_URI = 2;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button writeBtn = (Button)findViewById(R.id.writeBtn);
        writeBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String msg = messageInput.getText().toString();
				int type = TYPE_TEXT;
				if(rbutton02.isChecked()){
					type = TYPE_URI;
				}
				
				NdefMessage mMessage = createTagMessage(msg, type);
				byte[] messageBytes = mMessage.toByteArray();
				
				String hexStr = bytesToHex0x(messageBytes);
				messageOutput.setText(hexStr);
				
				showTag(mMessage);
			}
		});
        
        messageInput = (EditText)findViewById(R.id.messageInput);
        messageOutput = (TextView)findViewById(R.id.messageOutput);
        tagOutput = (TextView)findViewById(R.id.tagOutput);
        
        rgroup01 = (RadioGroup)findViewById(R.id.rgroup01);
        rbutton01 = (RadioButton) findViewById(R.id.rbutton01);
        rbutton02 = (RadioButton) findViewById(R.id.rbutton02);
	}
	
	private NdefMessage createTagMessage(String msg, int type){
	//todo	
		NdefRecord[] records = new NdefRecord[1];
		
		if(type == TYPE_TEXT){
			records[0] = createTextRecord(msg, Locale.KOREAN, true);
		}else if(type == TYPE_URI){
			records[0] = createUriRecord(msg.getBytes());
		}
		
		NdefMessage mMessage = new NdefMessage(records);
		
		return mMessage;
		
	}
	
	private NdefRecord createTextRecord(String text, Locale locale, boolean encodeInUtf8) {
        final byte[] langBytes = locale.getLanguage().getBytes(Charsets.US_ASCII);
        final Charset utfEncoding = encodeInUtf8 ? Charsets.UTF_8 : Charset.forName("UTF-16");
        final byte[] textBytes = text.getBytes(utfEncoding);
        final int utfBit = encodeInUtf8 ? 0 : (1 << 7);
        final char status = (char) (utfBit + langBytes.length);
        final byte[] data = Bytes.concat(new byte[] {(byte) status}, langBytes, textBytes);
        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    }
	
	private NdefRecord createUriRecord(byte[] data) {
        return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI, new byte[0], data);
    }
	
	private void showTag(NdefMessage mMessage){
	//todo
		List<ParsedRecord> records = NdefMessageParser.parse(mMessage);
		
		final int size = records.size();
		for(int i=0; i<size; i++){
			ParsedRecord record = records.get(i);
			
			int type = record.getType();
			String recordStr = "";
			if(type == ParsedRecord.TYPE_TEXT){
				recordStr = "TEXT: " + ((TextRecord)record).getText() + "\n";
			}else if(type == ParsedRecord.TYPE_URI){
				recordStr = "Uri" + ((UriRecord)record).getUri().toString();
			}
			
			tagOutput.append(recordStr);
			tagOutput.invalidate();
		}
	}
	
	
	public static String stringToHex(String s) {
        String result = "";

        for (int i = 0; i < s.length(); i++) {
          result += String.format("%02X ", (int) s.charAt(i));
        }

        return result;
      }

    public static String stringToHex0x(String s) {
        String result = "";

        for (int i = 0; i < s.length(); i++) {
          result += String.format("0x%02X ", (int) s.charAt(i));
        }

        return result;
    }

    public static String bytesToHex0x(byte[] s) {
        String result = "";

        for (int i = 0; i < s.length; i++) {
          result += String.format("0x%02X ", (int) s[i]);
        }

        return result;
    }

}
